#include "TCPServerChannel.h"

namespace network
{

TCPServerChannel::TCPServerChannel(std::string& name, UInt32 id, std::function<void(const TCPServerDispatcherPacket::Ptr&)> handlePacket)
: Task("TCPServerChannel")
, _name(name)
, _nChannelID(id)
, _handlePacket(handlePacket)
{
}

TCPServerChannel::~TCPServerChannel()
{
}

void TCPServerChannel::runTask()
{
	if (_handlePacket == nullptr)
		return ;

	while (!isCancelled())
	{
		CustomNotification<TPacketParams> *pNotif = dynamic_cast<CustomNotification<TPacketParams>*>(_nq.waitDequeueNotification());
		if (pNotif)
		{
			const TCPServerDispatcherPacket::Ptr& packet = pNotif->custom().getPacket();
			_handlePacket(packet);
		}
	}
}

void TCPServerChannel::enquenceNotification(CustomNotification<TPacketParams>* pNotif)
{
	_nq.enqueueNotification(pNotif);
}

}
